package cn.hexcloud.dga.governance.assessor.calc;

import cn.hexcloud.dga.governance.assessor.Assessor;
import cn.hexcloud.dga.governance.bean.AssessParam;
import cn.hexcloud.dga.governance.bean.GovernanceAssessDetail;
import cn.hexcloud.dga.meta.bean.TableMetaInfo;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component("NO_OUTPUT")
public class NoOutputAssessor extends Assessor {
    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception {
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        // 读取阈值天数
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject metricParamsJsonObj = JSONObject.parseObject(metricParamsJson);
        Integer days = metricParamsJsonObj.getInteger("days");

        // 读取评估日期
        Date assessDate = DateUtils.parseDate(assessParam.getAssessDate(), "yyyy-MM-dd");

        // 读取最后更新日期
        Date lastModifyTime = DateUtils.truncate(tableMetaInfo.getTableLastModifyTime(), Calendar.DATE);
        long diffInMilli = assessDate.getTime() - lastModifyTime.getTime();
        long diffDays = TimeUnit.DAYS.convert(diffInMilli, TimeUnit.MILLISECONDS);
        if(diffDays > days){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem(String.format("长期没有输出，上次输出日期为%s,至今已有%d天", DateFormatUtils.format(lastModifyTime,"yyyy-MM-dd"),diffDays));
        }
    }
}
